home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Online / SpeakFreely / src / lpc10 / bitio.c < prev    next >
C/C++ Source or Header  |  2000-05-18  |  3KB  |  144 lines

  1. /* SpeedEdit 32,40,25,0,0,10,16,10 Updated 02/04/93 15:07:36 */
  2. /************************************************************************
  3. *
  4. *    BITIO Version 55
  5. *
  6. ************************************************************************/
  7. #include <stdio.h>
  8. #include "lpcdefs.h"
  9.  
  10. #define STRLEN 80
  11.  
  12. static char hex[24]="0123456789ABCDEFabcdef ";
  13.  
  14. int bitio(which, fd, ibits, n)
  15. int which, n, ibits[];
  16. FILE *fd;
  17. {
  18. char str[STRLEN];
  19. int bitsrd, bitswr, return_value, i;
  20.  
  21. switch(which)    {
  22. /************************************************************************
  23. *   Read a frame from bitstream file
  24. ************************************************************************/
  25.   case 1:
  26.         if(fscanf(fd, "%s",str) != EOF )        
  27.     {
  28.       bitsrd = gethx(str, ibits, n);
  29.       return_value = bitsrd;
  30.     }
  31.     else
  32.       return_value = 0;
  33.     break;
  34.  
  35. /************************************************************************
  36. *   Write a frame to bitstream file
  37. ************************************************************************/
  38.   case 2:
  39.     bitswr = puthx(str, ibits, n);
  40.     for(i=0;i<(n+3)/4;i++)
  41.           fprintf(fd, "%c",str[i]);
  42.         fprintf(fd, "\n");
  43.   break;
  44. }
  45. return return_value;
  46. }
  47.  
  48.  
  49. /************************************************************************
  50. *   Read bits from hex digit stream
  51. ************************************************************************
  52. *
  53. *   Skip leading blanks, split hex digits into individual bits,
  54. *  terminate after getting n bits or finding non-hex character.
  55. *  Return value = number of bits in input record (which could be
  56. *  more or less than n).
  57. */
  58.  
  59. int gethx(str, ibits, n)
  60. char *str;
  61. int ibits[], n;
  62. {
  63. int gethx, puthx;
  64. int ib, ic, i, ii, j, k, nc;
  65. static int first=1;
  66.  
  67.     /*if(first)    {
  68.       first=0;
  69.           strcpy(hex, "0123456789ABCDEFabcdef ");
  70.     }*/
  71.  
  72.     /* count the number of valid characters in str */
  73.  
  74.     ic = 0;
  75.     for( j = 0;j< strlen(str);j++)    {
  76.         k = the_index(hex, str[j]);
  77.         if ((k < 0) || ((k > 21) && (ic > 0))) break;
  78.         if (k<=21) ic++;
  79.     }
  80.  
  81.  
  82.     ib = -1;
  83.     j = j - ic;
  84.     nc = mmin((n+3)/4, ic);
  85.     for( i = 0;i<= nc-1;i++)    {
  86.         k = the_index(hex, str[i+j]);
  87.         if ((k < 0) || (k > 21)) 
  88.                 printf("stop 'gethx: internal error'\n");
  89.         if (k > 15) k -= 6;
  90.         for( ii = 1 + mmax(0, 4*(nc-i)-n);ii<= 4;ii++)    {
  91.         ib++;
  92.         /*ibits(ib) = and(ishft(k, ii-4), 1)*/
  93.         ibits[ib] = (k >> (4-ii)) & 1;
  94.         }
  95.     }
  96.  
  97.     gethx = ib;
  98.     if (ic > nc) gethx = 4*ic;
  99.     
  100.     return gethx;
  101. }
  102.  
  103. /************************************************************************
  104. *   Write bits to hex digit stream
  105. ************************************************************************/
  106.  
  107. int puthx(str, ibits, n)
  108. char str[STRLEN];
  109. int ibits[], n;
  110. {
  111. int ib, nc, ic, k, j;
  112.  
  113.     ib = -1;
  114.     nc = (n+3) / 4;
  115.     for (ic = 1;ic<= mmin(STRLEN, nc);ic++) {
  116.         k = 0;
  117.         for (j = 1; j<= mmin(n-4*(nc-ic), 4); j++)    {
  118.         ib++;
  119.         /*k = or(ishft(k,1), and(ibits(ib),1))*/
  120.         k = (k<<1) | (ibits[ib] & 1);
  121.         }
  122.         str[ic-1] = hex[k];
  123.     }
  124.  
  125.     return ib;
  126. }
  127.  
  128.  
  129.  
  130.  
  131.  
  132. int the_index(string,character)
  133. char string[],character;
  134. {
  135. int i;
  136.  
  137. for(i=0;i<strlen(string);i++)
  138.     if(string[i] == character)
  139.         break;
  140.  
  141. return(i);
  142.  
  143. }
  144.